//============================================================================================
/**
 * @file	zukan.c
 * @brief	金銀図鑑画面
 * @author	Hiroyuki Nakamura
 * @date	08.08.26
 */
//============================================================================================
#include "common.h"

//#include "savedata/config.h"
#include "application/zukanlist/zkn_gira.h"
#include "zkn2_main.h"
#include "zkn2_seq.h"
#include "zkn2_list.h"

#include "AMProtect/AMProtect.h"

FS_EXTERN_OVERLAY(amprotect);

static void MagiconFunc1(void);
static void MagiconFunc2(void);
static void NotMagiconFunc(void);

//--------------------------------------------------------------------------------------------
/**
 * プロセス関数：初期化
 *
 * @param	proc	プロセスデータ
 * @param	seq		シーケンス
 *
 * @return	処理状況
 */
//--------------------------------------------------------------------------------------------
PROC_RESULT ZUKAN_ProcInit( PROC * proc, int * seq )
{
	ZKN2_WORK * wk;

	OS_Printf( "↓↓↓↓↓　図鑑処理開始　↓↓↓↓↓\n" );

	sys_CreateHeap( HEAPID_BASE_APP, HEAPID_ZUKAN, 0x61000 );

	wk = PROC_AllocWork( proc, sizeof(ZKN2_WORK), HEAPID_ZUKAN );
	MI_CpuClear8( wk, sizeof(ZKN2_WORK) );

	wk->dat = PROC_GetParentWork( proc );

	wk->next_seq = ZKN2SEQ_MAINSEQ_START;

	// 全国図鑑保持フラグ設定
	wk->zkn_mode = ZUKANPARAM_ModeGet( wk->dat->zkn_param );

	if( ZukanWork_GetZenkokuZukanFlag( wk->dat->zukan_work ) ){
		wk->zenkoku_mode = TRUE;
		if( wk->zkn_mode == ZKN2_MODE_NAZO ){
			wk->zkn_mode = ZKN2_MODE_ZENKOKU;
		}
	}else{
		wk->zenkoku_mode = FALSE;
		if( wk->zkn_mode == ZKN2_MODE_NAZO ){
			wk->zkn_mode = ZKN2_MODE_JOHTO;
		}
	}

	// 参照データを設定
	if( ZukanWork_GetPokeGetFlag( wk->dat->zukan_work, MONSNO_KIMAIRAN ) == TRUE ){
		ZKN_GIRA_Init( ZukanWork_GetPokeForm(wk->dat->zukan_work,MONSNO_KIMAIRAN,0) );
	}else{
		ZKN_GIRA_Init( 0 );		// ギラティナを捕まえてないのでどっちでも良い
	}

//	wk->zenkoku_mode = TRUE;
//	wk->zkn_mode = ZKN2_MODE_ZENKOKU;

//	OS_Printf( "自機座標 : x = %d, z = %d\n", wk->dat->player_x, wk->dat->player_z );

	Snd_PlayerSetPlayerVolume( PLAYER_FIELD, BGM_VOL_ZUKAN );

	wk->lang = PM_LANG;

	return PROC_RES_FINISH;
}

//--------------------------------------------------------------------------------------------
/**
 * プロセス関数：メイン
 *
 * @param	proc	プロセスデータ
 * @param	seq		シーケンス
 *
 * @return	処理状況
 */
//--------------------------------------------------------------------------------------------
PROC_RESULT ZUKAN_ProcMain( PROC * proc, int * seq )
{
	ZKN2_WORK * wk = PROC_GetWork( proc );

	if( ZKN2SEQ_Main( wk, seq ) == FALSE ){
		return PROC_RES_FINISH;
	}

	return PROC_RES_CONTINUE;
}

//--------------------------------------------------------------------------------------------
/**
 * プロセス関数：終了
 *
 * @param	proc	プロセスデータ
 * @param	seq		シーケンス
 *
 * @return	処理状況
 */
//--------------------------------------------------------------------------------------------
PROC_RESULT ZUKAN_ProcEnd( PROC * proc, int * seq )
{
	ZKN2_WORK * wk = PROC_GetWork( proc );

	// --------------------------------------------------
#if MAGICON_CHECK

    FS_LoadOverlay( MI_PROCESSOR_ARM9, FS_OVERLAY_ID(amprotect) );

    if (AM_IsMagiconA2(MagiconFunc1)) {
		sys_AllocMemoryLo( HEAPID_BASE_APP, 1000 );
    }
#endif
	// --------------------------------------------------



	// 外部保持パラメータセット
	ZUKANPARAM_Set( wk->dat->zkn_param, ZKN2LIST_SelMonsGet(wk), wk->zkn_mode );

	// --------------------------------------------------
#if MAGICON_CHECK
    if (AM_IsMagiconA1(MagiconFunc2)) {
		sys_AllocMemoryLo( HEAPID_BASE_APP, 1000 );
    }
#endif
	// --------------------------------------------------

	PROC_FreeWork( proc );

	sys_DeleteHeap( HEAPID_ZUKAN );

	Snd_PlayerSetPlayerVolume( PLAYER_FIELD, BGM_VOL_MAX );

	OS_Printf( "↑↑↑↑↑　図鑑処理終了　↑↑↑↑↑\n" );

	// SEバンクの解放
	Snd_SubSceneClear( );

	// --------------------------------------------------
#if MAGICON_CHECK

	if (!AM_IsNotMagiconA3(NotMagiconFunc)) {
		sys_AllocMemoryLo( HEAPID_BASE_APP, 1000 );
    }

	FS_UnloadOverlay(MI_PROCESSOR_ARM9, FS_OVERLAY_ID(amprotect));
#endif
	// --------------------------------------------------


	return PROC_RES_FINISH;
}

//--------------------------------------------------------------------------------------------
/**
 * @brief	不正対策メモリーアロック関数その1
 */
//--------------------------------------------------------------------------------------------
static void MagiconFunc1(void)
{
	sys_AllocMemoryLo( HEAPID_BASE_APP, 1000 );
}

//--------------------------------------------------------------------------------------------
/**
 * @brief	不正対策メモリーアロック関数その1
 */
//--------------------------------------------------------------------------------------------
static void MagiconFunc2(void)
{
	sys_AllocMemoryLo( HEAPID_BASE_APP, 1000 );
}

//--------------------------------------------------------------------------------------------
/**
 * @brief	正規ロム関数
 */
//--------------------------------------------------------------------------------------------
static void NotMagiconFunc(void)
{
}